package demo1;

import java.util.Comparator;

//接口名叫Comparable
//重写方法叫 compareTo
//class demo1.Student implements Comparable<demo1.Student>{
//    String name;
//    int age;
//    public demo1.Student(String name, int age) {
//        this.name = name;
//        this.age = age;
//    }
//
//    @Override
//    public String toString() {
//        return "demo1.Student{" +
//                "name='" + name + '\'' +
//                ", age=" + age +
//                '}';
//    }
//    @Override
//    public int compareTo(demo1.Student o) {
//        if(this.age > o.age) {
//            return 1;
//        } else if(this.age == o.age) {
//            return 0;
//        } else {
//            return -1;
//        }
//    }
//}
//class Student{
//    String name;
//    int age;
//    public Student(String name, int age) {
//        this.name = name;
//        this.age = age;
//    }
//
//    @Override
//    public String toString() {
//        return "demo1.Student{" +
//                "name='" + name + '\'' +
//                ", age=" + age +
//                '}';
//    }
//}
////比较器-->比较灵活
////根据年龄比较
//class AgeComparator implements Comparator<Student>{
//    @Override
//    public int compare(Student student1,Student student2 ) {
//        return student1.age-student2.age;
//    }
//}
////根据姓名比较
//class NameComparator implements Comparator<Student>{
//    @Override
//    public int compare(Student student1,Student student2 ) {
//        return student1.name.compareTo(student2.name);
//    }
//}
//public class Main {
//    public static void main(String[] args) {
//        AgeComparator acpt = new AgeComparator();
//        NameComparator ncpt = new NameComparator();
//
//
//        Student student1 = new Student("张三",12);
//        Student student2 = new Student("李四",8);
//
//        System.out.println(acpt.compare(student1, student2));
//        System.out.println(ncpt.compare(student1, student2));
////        System.out.println(student1.compareTo(student2));
//        // System.out.println("Hello world!");
//    }
//}
